<?php
/* 执行一个sql返回一个字符串 */
function saveData($data) {
	//要传入 主键 和外键
	$id = $_POST['id'];
	$tablename = $databasepre."general";
	$insertStr1 = "";
	$insertStr2 = "";
	$updateStr = "";

	$selectSQL = "select lower(column_name) as column_name ,lower(data_type) as column_type,character_maximum_length as column_length  from  information_schema.columns where table_name='".$tablename."'";
	echo $selectSQL;
	
	$query = mysql_query($selectSQL);
	$i = 0;
	while($row=mysql_fetch_array($query))
	{
		$sep = ",";
		$column_type = $row['column_type'];
		$column_name = $row['column_name'];
		$column_value = "";
		if(isset($_POST["$column_name"])){
		echo "==".$column_name;
			$column_value = mysql_real_escape_string($_POST["$column_name"]);
		}
		if(isset($_POST["$column_name"]) ){//isset($_POST["$column_name"])  && $column_value!=""
			if($i==0){
				$i +=1;
				$sep = "";
			}
			$insertStr1 .= $sep.$column_name;
			if($column_type=="varchar" || $column_type=="text" || $column_type=="datetime"|| $column_type=="date"){
				$insertStr2 .= $sep."'".$column_value."'";
				$updateStr .= $sep.$column_name."='".$column_value."'";
			}else if($column_type == "int"){
				$insertStr2 .= $sep."'".$column_value."'";
				$updateStr .= $sep.$column_name."='".$column_value."'";
			}
		}
	}
	if($id==0 || $id==""){
		$insertSQL = "insert into ".$tablename."(".$insertStr1.",createdate) values (".$insertStr2.",now())";
		//echo $insertSQL;
		mysql_query($insertSQL);
		$id = mysql_insert_id();
	}else{
		$updateSQL = "update ".$tablename." set ".$updateStr.",updatedate=now() where id=".$id;
		echo $updateSQL;
		mysql_query($updateSQL);
	}
	
	$tip ="保存成功！";

}

?>